<?php


function excel_hari_kerja($periode_id){
	$header = array(
		'NIK'=>array(),  
		'Nama'=>array(),   
		'IN'=>array(),  
		'PP'=>array(),  
		'BT'=>array(),  
		'BL'=>array(),  
		'BP'=>array(),  
		'SD'=>array(), 
		'C'=>array(), 
	);
	
	$query = " SELECT * FROM tmn_hari_kerja a
				INNER JOIN karyawan b ON b.karyawan_id = a.karyawan_id 
				WHERE  b.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
				AND b.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  ) 	
				AND periode_id = {$periode_id}
			";	
	$result = my_query($query);
	while($ey = my_fetch_array($result )){ 
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		$row[] = array( 
			'nik'=>  "'".sprintf('%07d',$ey['nik']),    
			'nama'=>ucfirst($karyawan['nama']),
				'in'=>  $ey['in']  ,
				'pp'=>  $ey['pp'],
				'bt'=>  $ey['bt'],
				'bl'=> $ey['bl']  ,
				'bp'=>   $ey['bp'] ,
				'sd'=>  $ey['sd'] ,
				'c'=>  $ey['c']   
		);
	}
	
	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas ,4 ,false ); 	
}


function list_karyawan_hari_kerja( $periode_id ){
	$periode_gaji_id = get_periode_gaji() ;
	if($periode_id==0){
		$periode_id=$periode_gaji_id ;
	}
	
	my_set_code_js('
		function confirmRst(){
			var t = confirm(\'Yakin akan reset data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=rst\';
			}
			return false;
		}
	');
	$header = array(
		'NIK'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'Nama'=>array('style'=>'text-align:left;border-bottom:2px solid;width:28%'),  
		'Jabatan'=>array('style'=>'text-align:left;border-bottom:2px solid;width:35%'),  
		'IN'=>array('style'=>'text-align:center;border-bottom:2px solid;width: 4%'),  
		'PP'=>array('style'=>'text-align:center;border-bottom:2px solid;width:4%'), 
		'BT'=>array('style'=>'text-align:center;border-bottom:2px solid;width:4%'), 
		'BL'=>array('style'=>'text-align:center;border-bottom:2px solid;width:4%'), 
		'BP'=>array('style'=>'text-align:center;border-bottom:2px solid;width:4%'), 
		'SD'=>array('style'=>'text-align:center;border-bottom:2px solid;width:4%'), 
		'C'=>array('style'=>'text-align:center;border-bottom:2px solid;width:4%'), 
	);
	$row = array(); 
	$paging =''; 
	$nik_start = isset($_GET['nik_start']) ? $_GET['nik_start'] : '';
	$nik_end = isset($_GET['nik_end']) ? $_GET['nik_end'] : '';
	if(isset($_GET['nik_start'])  && isset($_GET['nik_end']) ){
		$query = "
			SELECT    * FROM tmn_hari_kerja a
				INNER JOIN karyawan b ON b.karyawan_id = a.karyawan_id 
			INNER JOIN biodata c ON b.biodata_id = c.biodata_id 
			WHERE    a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
			AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian) 
			AND(  b.nik BETWEEN '{$_GET['nik_start']}' AND '{$_GET['nik_end']}' )
				AND a.periode_id = {$periode_id}
		";
	}else{	
		$query = " SELECT * FROM tmn_hari_kerja a
				INNER JOIN karyawan b ON b.karyawan_id = a.karyawan_id 
				WHERE  b.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
				AND b.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  )  
				AND a.periode_id = {$periode_id}
			";	
	}
	$result = my_query($query);
	$total_records = my_num_rows($result );
	$scroll_page =7;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		  
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&periode_id={$_GET['periode_id']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	
	$kgPagerOBJ = new kgPager();
			$kgPagerOBJ->pager_set(
				$pager_url, 
				$total_records, 
				$scroll_page, 
				$per_page, 
				$current_page, 
				$inactive_page_tag, 
				$previous_page_text, 
				$next_page_text, 
				$first_page_text, 
				$last_page_text ,$pager_url_last); 
				
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	
	$i = ($current_page  - 1 ) * 25; 
	
		while($ey = my_fetch_array($result)){
			 $i++;
			$karyawan = loaddata_karyawan($ey['karyawan_id']);
			if(empty($karyawan['gelar'])){
				$nama_karyawan=ucfirst($karyawan['nama']) ;
			}else{
				$nama_karyawan=trim(ucfirst($karyawan['nama'])).", ".$karyawan['gelar'] ;
			}

			$jabatan = my_get_data_by_id('jabatan' ,'jabatan_id' , ($karyawan['jabatan_id']<>'' ? $karyawan['jabatan_id'] : 0 ));
			
			 $row[] = array(
				'nik'=>position_text_align ($karyawan['nik'] ,'center'),    
				'gd'=> $nama_karyawan ,      
				'jabatan'=> $jabatan['nama_jabatan']   ,
				'in'=>position_text_align(  $ey['in']  ,'center'),
				'pp'=> position_text_align( $ey['pp'],'center'),
				'bt'=> position_text_align( $ey['bt'],'center'),
				'bl'=>position_text_align( $ey['bl']  ,'center'),
				'bp'=> position_text_align( $ey['bp'] ,'center'),
				'sd'=> position_text_align( $ey['sd'] ,'center'),
				'c'=> position_text_align( $ey['c']   ,'center')
			);
		}
	$paging = $kgPagerOBJ ->showPaging();	
	 
	$datas = table_rows($row); 
	 $view = form_header( "..." , "..."  );
	$query_check_periode = " SELECT * FROM pr_periode where periode_id <= {$periode_gaji_id} ORDER BY periode_id DESC";
	$result = my_query($query_check_periode);
	$prs = array();
	while($prow = my_fetch_array($result)){
		$prs[$prow['periode_id']] = date('m-Y',strtotime($prow['tanggal']));
	}
	$fperiode_id = array(
		'id'=>'periode_id',
		'value'=>$periode_id ,
		'name'=>'periode_id',
	);
	$view .= '<input type="hidden"  value="'.$_GET['com'].'" name="com" />';
	$view .= form_field_display( form_dropdown($fperiode_id,$prs)   , "Periode Gaji"   );	 
	$form_start = '<input type="text" size="7" value="'.$nik_start.'" name="nik_start" />';
	$form_end = '<input type="text" size="7" value="'.$nik_end.'" name="nik_end" />';
	$view .= form_field_display( $form_start.' s/d '.$form_end  , "NIK Range"   );
	$submit = array(
		'value' => (  ' Check '  ),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 	
	$cetak = array(
		'value' => (  ' Cetak '  ),
		'onclick'=> ( 
					$i > 0 ? 
					'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=print_tmn_hari_kerja&periode_id='.$periode_id.'&nik_start='.$_GET['nik_start'].'&nik_end='.$_GET['nik_end'].'\' ' :
					'javascript:alert(\'No data available\');' ),
		
		'type'=>'button','class'=>'main_button'
	);
	$form_cetak= form_dynamic($cetak); 	
 
	$excel = array(
		'value' => (  ' Excel '  ),
		'onclick' => 'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=excel_hari_kerja&periode_id='.$periode_id.'&nik_start='.$nik_start.'&nik_end='.$nik_end.'\'', 
		'type'=>'button','class'=>'main_button'
	);
	$form_excel= form_dynamic($excel); 
	$view .= form_field_display( $form_submit .'  '.$form_excel.'  '.$form_cetak , "&nbsp;" ,  "" );
	$view .= form_footer( );
	$view = str_replace("method=\"post\"","method=\"get\"",$view);
			return  $view.   table_builder($header , $datas ,  6 , false ,$paging );	

}


function total_masuk($karyawan_id , $periode_id  ){
	$query = "SELECT COUNT(*) AS ttl FROM  tmn_hari_kerja WHERE periode_id = {$periode_id} AND karyawan_id = {$karyawan_id} "; 
	$result = my_query($query);
	$row = my_fetch_array($result);
	return $row['ttl'];
}

